/*
 *	
 *	- trace tab
 *	- status: errors, warnings and info
 *	- progress bar
 *	- threads
 */

package text.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import text.util.DBConnector;
import text.ia.InputAnalyzer;
import text.obj.AlignedSentence;
import text.obj.AlignedChunk;
import text.obj.AlignedTemplate;
import text.obj.TExtTemplate;
import text.learning.TExtLearner;

public class TrainingModeViewer extends Viewer implements ActionListener {

	private JFileChooser fc = null;
	private JTabbedPane tabbedPane = null;
	private JTextArea sourceTextArea = null;
	private JTextArea targetTextArea = null;
	private JComboBox sourceCombo = null;
	private JComboBox targetCombo = null;
	private String sourcePath = null;
	private String targetPath = null;
	private char caretMode = '\0';
	private TExtLearner txtLearner = null;
	private JTabbedPane traceTab = null;
	private static Vector vTraceData = null;

	public void setEnabled(boolean bEnabled) {
		sourceTextArea.setEnabled(bEnabled);
		targetTextArea.setEnabled(bEnabled);
		sourceCombo.setEnabled(bEnabled);
		targetCombo.setEnabled(bEnabled);
	}
	
	public static Vector getTraceData() {
		return vTraceData;
	}
	
    /**
     * main method allows us to run as a standalone demo.
     */
    public static void main(String[] args) {
		TrainingModeViewer trainingModeViewer = new TrainingModeViewer();
		trainingModeViewer.init();
    }
    
    /**
     * TrainingModeViewer Constructor
     */
    public TrainingModeViewer() {
        // Set the title for this demo, and an icon used to represent this
        // demo inside the GUI app.
        super(null, "TrainingModeViewer", "trainingMode.GIF");
        
        createTrainingModeUI();
    }
    
    public void createTrainingModeUI() {
		tabbedPane = createTabbedPane();
		tabbedPane.setBorder(null);

    	//Training Mode Tab
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, createSourcePanel(), createTargetPanel());

		splitPane.setDividerLocation(245);
		splitPane.setOneTouchExpandable(true);

		tabbedPane.addTab("Training Mode", splitPane);
        
        getViewerPanel().add(tabbedPane, BorderLayout.CENTER);
	}

	private JTabbedPane createTabbedPane() {
		return new JTabbedPane(JTabbedPane.TOP) {
			protected void paintComponent(Graphics g) {
				Color orig = g.getColor();
				Rectangle tabBounds = getBoundsAt(0);
				int tabLowerY = tabBounds.y + tabBounds.height;

				g.setColor(new Color(247, 243, 233));
				g.fillRect(0, 0, getWidth(), tabLowerY);

				g.setColor(orig);
				super.paintComponent(g);

				g.setColor(Color.WHITE);
				g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
				g.drawRect(1, tabLowerY - 1, getWidth() - 3, getHeight() - tabLowerY - 1);
				g.setColor(orig);
			}
		};
	}

	private JPanel createSourcePanel() {
		JPanel sourcePanel = new JPanel(new BorderLayout());

		JPanel sourceTextPanel = new JPanel(new BorderLayout());
		sourcePanel.add(sourceTextPanel, BorderLayout.CENTER);
		
		JPanel sourceComboPanel = new JPanel();
		sourceTextPanel.add(sourceComboPanel, BorderLayout.NORTH);

		sourceComboPanel.setPreferredSize(new Dimension(184, 36));

		JLabel sourceLabel = new JLabel();
		sourceComboPanel.add(sourceLabel);
		sourceLabel.setText("Source Language");

		ComboBoxModel sourceComboModel = new DefaultComboBoxModel(
			new String[] { "English", "Filipino" });
		
		sourceCombo = new JComboBox();
		sourceComboPanel.add(sourceCombo);
		sourceCombo.setModel(sourceComboModel);
		sourceCombo.addActionListener(this);

		JScrollPane sourceScrollPane = new JScrollPane();
		sourceTextPanel.add(sourceScrollPane, BorderLayout.CENTER);

		sourceTextArea = new JTextArea();
		sourceTextArea.setLineWrap(true);
		sourceTextArea.setWrapStyleWord(true);
		sourceTextArea.setFont(new Font("Courier", Font.PLAIN, 12));
		sourceTextArea.setBorder(new TitledBorder("Source: Untitled"));
		sourceTextArea.getDocument().addDocumentListener(new ChangeAction(sourceTextArea));
		sourceTextArea.addCaretListener(new CaretAction(this));

		sourceScrollPane.setViewportView(sourceTextArea);
						
		return sourcePanel;	
	}
	
	private JPanel createTargetPanel() {
		JPanel targetPanel = new JPanel(new BorderLayout());

		JPanel targetTextPanel = new JPanel(new BorderLayout());
		targetPanel.add(targetTextPanel, BorderLayout.CENTER);
		
		JPanel targetComboPanel = new JPanel();
		targetTextPanel.add(targetComboPanel, BorderLayout.NORTH);

		targetComboPanel.setPreferredSize(new Dimension(184, 36));

		JLabel targetLabel = new JLabel();
		targetComboPanel.add(targetLabel);
		targetLabel.setText("Target Language");

		ComboBoxModel targetComboModel = new DefaultComboBoxModel(
			new String[] { "Filipino", "English" });
		
		targetCombo = new JComboBox();
		targetComboPanel.add(targetCombo);
		targetCombo.setModel(targetComboModel);
		targetCombo.addActionListener(this);

		JScrollPane targetScrollPane = new JScrollPane();
		targetTextPanel.add(targetScrollPane, BorderLayout.CENTER);

		targetTextArea = new JTextArea();
		targetTextArea.setLineWrap(true);
		targetTextArea.setWrapStyleWord(true);
		targetTextArea.setFont(new Font("Courier", Font.PLAIN, 12));
		targetTextArea.setBorder(new TitledBorder("Target: Untitled"));
		targetTextArea.getDocument().addDocumentListener(new ChangeAction(targetTextArea));
		targetTextArea.addCaretListener(new CaretAction(this));

		targetScrollPane.setViewportView(targetTextArea);
						
		return targetPanel;	
	}
	
	private JPanel createExtractedTemplates() {
		JPanel extractedTemplatesTab = new JPanel(new BorderLayout());
		JScrollPane extractedTemplatesScrollPane = new JScrollPane();
		extractedTemplatesTab.add(extractedTemplatesScrollPane, BorderLayout.CENTER);

		Vector vColumnNames = new Vector();
		vColumnNames.add("Template ID");	//query
		vColumnNames.add("Method (Template Derivation)");	//dts|dtr|dtc
		vColumnNames.add("Current Source (Template Derivation)");	//current aligned sentence | chunk | template
		vColumnNames.add("Matching Source (Template Derivation)");	//matching aligned sentence | chunk | template
		vColumnNames.add(sourceCombo.getSelectedItem()+" Template");
		vColumnNames.add(targetCombo.getSelectedItem()+" Template");

		Vector vData = new Vector();
		Vector vRow = new Vector();
		Vector vContent = null;
		Vector vTemplates = null;
		Vector vChunks = null;
		AlignedSentence aS = null;
		AlignedTemplate aT = null;
		AlignedChunk aC = null;
		StringBuffer strbMsg = null;

		if(vTraceData != null) {
			for(int i = 0; i < vTraceData.size(); i++) {
				vRow = new Vector();

				if(((String)vTraceData.get(i)).equals("TR")) {
					i++;
					vContent = (Vector)vTraceData.get(i);
					aT = (AlignedTemplate) ((Vector)vContent.get(2)).get(0);
					vRow.add(getTemplateID((TExtTemplate)aT.getSource(), (TExtTemplate)aT.getTarget()));
					vRow.add("Template Refinement");
					aS = (AlignedSentence) vContent.get(0);
					vRow.add(aS.getSource()+"<->"+aS.getTarget());
					aT = (AlignedTemplate) vContent.get(1);
					vRow.add(aT.getSource()+"<->"+aT.getTarget());
					aT = (AlignedTemplate) ((Vector)vContent.get(2)).get(0);
					vRow.add(aT.getSource());
					vRow.add(aT.getTarget());
					vData.add(vRow);
				}else if(((String)vTraceData.get(i)).equals("DTS")) {
					i++;
					vContent = (Vector)vTraceData.get(i);
					aT = (AlignedTemplate) vContent.get(2);
					vRow.add(getTemplateID((TExtTemplate)aT.getSource(), (TExtTemplate)aT.getTarget()));
					vRow.add("Sentence");
					aS = (AlignedSentence) vContent.get(0);
					vRow.add(aS.getSource()+"<->"+aS.getTarget());
					aS = (AlignedSentence) vContent.get(1);
					vRow.add(aS.getSource()+"<->"+aS.getTarget());
					vRow.add(aT.getSource());
					vRow.add(aT.getTarget());
					vData.add(vRow);
				}else if(((String)vTraceData.get(i)).equals("DTC")) {
					i++;
					vContent = (Vector)vTraceData.get(i);
					System.out.println("vContent: "+vContent);
					vTemplates = (Vector) vContent.get(1);
					vChunks = (Vector) vContent.get(2);
					aS = (AlignedSentence) vContent.get(0);
						
					for(int j = 0; vTemplates != null && j < vTemplates.size(); j++) {
						vRow = new Vector();
						aT = (AlignedTemplate) vTemplates.get(j);
						vRow.add(getTemplateID((TExtTemplate)aT.getSource(), (TExtTemplate)aT.getTarget()));
						vRow.add("Chunk Learning");
						//current source
						vRow.add(aS.getSource()+"<->"+aS.getTarget());
						//matching source: chunks
						//->* edit in TExtLearner.java -> bcoz of: 'LIKE' in domain
						strbMsg = new StringBuffer();
						for(int k = 0; vChunks != null && k < vChunks.size(); k++) {
							aC = (AlignedChunk) vChunks.get(k);
							strbMsg.append(aC.getSource()+"<->"+aC.getTarget());
							strbMsg.append("; ");
						}
						vRow.add(strbMsg.toString());
						//src template: templates
						vRow.add(aT.getSource());
						//tar template: templates
						vRow.add(aT.getTarget());
						vData.add(vRow);
					}
				}//else ignore
			}
		}

		TableModel templatesTableModel = new DefaultTableModel(vData, vColumnNames);

        TableSorter sorter = new TableSorter(templatesTableModel);
		JTable templatesTable = new JTable(sorter);
        sorter.setTableHeader(templatesTable.getTableHeader());
		templatesTable.getTableHeader().setReorderingAllowed(false);
		templatesTable.setDragEnabled(true);
		extractedTemplatesScrollPane.setViewportView(templatesTable);
		
		return extractedTemplatesTab;
	}

	private String getTemplateID(TExtTemplate tSrc, TExtTemplate tTar) {
		String strTID = null;
		
		//query
		String	strQuery = "SELECT templateID FROM Templates WHERE "
			+ "template" + sourceCombo.getSelectedItem() + " = '"
			+ tSrc.toString().replace("'","''") + "' AND "
			+ "template" + targetCombo.getSelectedItem() + " = '"
			+ tTar.toString().replace("'","''") + "' AND "
			+ "compID" + sourceCombo.getSelectedItem() + " = '"
			+ tSrc.getCompoundIdentifier() + "' AND "
			+ "compID" + targetCombo.getSelectedItem() + " = '"
			+ tTar.getCompoundIdentifier() + "'";
		
			
		DBConnector.connect();
		ResultSet rsAll = DBConnector.select(strQuery);
		
		try{
			while(rsAll.next()) {
				strTID = rsAll.getString("templateID");
			}
		}catch(SQLException e){
			e.printStackTrace();
		}

		DBConnector.close();
		
		if(strTID == null) {
			strTID = "refined";
		}
		
		return strTID;
	}

	private JPanel createGeneratedChunks() {
		JPanel generatedChunksTab = new JPanel(new BorderLayout());
		JScrollPane generatedChunksScrollPane = new JScrollPane();
		generatedChunksTab.add(generatedChunksScrollPane, BorderLayout.CENTER);

		Vector vColumnNames = new Vector();
		vColumnNames.add("Template ID");
		vColumnNames.add("Method (Chunk Generation)");
		vColumnNames.add("Template");
		vColumnNames.add("Domain");
		vColumnNames.add(sourceCombo.getSelectedItem()+" Chunk");
		vColumnNames.add(targetCombo.getSelectedItem()+" Chunk");

		Vector vData = new Vector();
		Vector vRow = new Vector();
		Vector vContent = null;
		Vector vChunks = null;
		AlignedSentence aS = null;
		AlignedTemplate aT = null;
		AlignedChunk aC = null;

		if(vTraceData != null) {
			for(int i = 0; i < vTraceData.size(); i++) {
				if(((String)vTraceData.get(i)).equals("TR")) {
					i++;
					vContent = (Vector)vTraceData.get(i);
					aT = (AlignedTemplate) ((Vector)vContent.get(2)).get(0);
					vChunks = (Vector) vContent.get(3);
					
					for(int j = 0; j < vChunks.size(); j++) {
						vRow = new Vector();
						vRow.add(getTemplateID((TExtTemplate)aT.getSource(), (TExtTemplate)aT.getTarget()));
						vRow.add("Template Refinement");
						vRow.add(aT.getSource()+"<->"+aT.getTarget());
						aC = (AlignedChunk) vChunks.get(j);
						
						vRow.add(aC.getSource().domain().substring(0,
				 				aC.getSource().domain().indexOf(".")));
						vRow.add(aC.getSource());
						vRow.add(aC.getTarget());
						vData.add(vRow);
					}
				}else if(((String)vTraceData.get(i)).equals("DTS")) {
					i++;
					vContent = (Vector)vTraceData.get(i);
					aT = (AlignedTemplate) vContent.get(2);
					vChunks = (Vector) vContent.get(3);
					
					for(int j = 0; j < vChunks.size(); j++) {
						vRow = new Vector();
						vRow.add(getTemplateID((TExtTemplate)aT.getSource(), (TExtTemplate)aT.getTarget()));
						vRow.add("Sentence");
						vRow.add(aT.getSource()+"<->"+aT.getTarget());
						aC = (AlignedChunk) vChunks.get(j);
						
						vRow.add(aC.getSource().domain());
						vRow.add(aC.getSource());
						vRow.add(aC.getTarget());
						vData.add(vRow);
					}
				}else if(((String)vTraceData.get(i)).equals("DTC")) {
					i++;
				}//else ignore
			}
		}

		TableModel chunksTableModel = new DefaultTableModel(vData, vColumnNames);

        TableSorter sorter = new TableSorter(chunksTableModel);
		JTable chunksTable = new JTable(sorter);
        sorter.setTableHeader(chunksTable.getTableHeader());
		chunksTable.getTableHeader().setReorderingAllowed(false);
		chunksTable.setDragEnabled(true);
		generatedChunksScrollPane.setViewportView(chunksTable);
		
		return generatedChunksTab;
	}
	
	public void actionPerformed(ActionEvent ae) {
		Object source = ae.getSource();
		
		if(source == sourceCombo) {
			if(sourceCombo.getSelectedIndex() == 0) {
				targetCombo.setSelectedIndex(0);
			}else {
				targetCombo.setSelectedIndex(1);
			}
		}
		if(source == targetCombo) {
			if(targetCombo.getSelectedIndex() == 0) {
				sourceCombo.setSelectedIndex(0);
			}else {
				sourceCombo.setSelectedIndex(1);
			}
		}
	}

	public void newAction(JTextArea tArea, String strType) {
		if(strType.charAt(0) == 'S') {
			//clear text area
			sourceTextArea.setText("");
			//clear stored path
			sourcePath = null;
		}else {
			//clear text area
			targetTextArea.setText("");
			//clear stored path
			targetPath = null;
		}
		//title
		tArea.setBorder(new TitledBorder(strType + "Untitled"));
	}

	public void openAction(JTextArea tArea, String strType) {
		fc = new JFileChooser();
		fc.setCurrentDirectory(new File("./resources/files/") );  
		TExtFileFilter filter = new TExtFileFilter();
		filter.addExtension("txt");
		fc.setFileFilter(filter);
		int returnVal = fc.showOpenDialog(getViewerPanel());
		
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File f = fc.getSelectedFile();
			BufferedReader bf = null;
			if (filter.accept(f)) {
				try{
		            bf = new BufferedReader(new FileReader(f) );
		            tArea.setText("");
		            String line;
		            while ( (line = bf.readLine() )!= null) {
		            	tArea.append(line +"\n");
		            }
		           	bf.close();
		            if(strType.charAt(0) == 'S') {
			            sourcePath = f.getPath();
		            }else {
			            targetPath = f.getPath();
		            }
					tArea.setBorder(new TitledBorder(strType + f.getName()));
				}catch(FileNotFoundException fnfe) {
				}catch(IOException ioe) {
				}catch(Exception ex) {
				}
			}else{
				System.out.println("File not accepted.");
			}
    	} 
	}

	public void saveAction(JTextArea tArea, String strType) {
		File f = null;
		if(strType.charAt(0) == 'S' && sourcePath != null) {
			f = new File(sourcePath);
			if(f.exists()) {
				saveFile(tArea, strType, f, strType+"Save");
			}else {
				saveFile(tArea, strType, null, strType+"Save");
			}
		}else if(targetPath != null) {
			f = new File(targetPath);
			if(f.exists()) {
				saveFile(tArea, strType, f, strType+"Save");
			}else {
				saveFile(tArea, strType, null, strType+"Save");
			}
		}else {
			saveFile(tArea, strType, null, strType+"Save");
		}
	}
	
	public void saveAsAction(JTextArea tArea, String strType) {
		saveFile(tArea, strType, null, strType+"Save As");
	}

	private void saveFile(JTextArea tArea, String strType, File file, String strTitle) {
		TExtFileFilter filter = new TExtFileFilter();
		filter.addExtension("txt");
		if(file == null) {
			//create or choose a file
			fc = new JFileChooser();
			fc.setCurrentDirectory(new File("./resources/files/") );  
			fc.setFileFilter(filter);
			fc.setDialogTitle(strTitle);
			int returnVal = fc.showSaveDialog(getViewerPanel());
			
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File f = fc.getSelectedFile();
				if(f.exists()) {
					int selectedIndex = JOptionPane.showConfirmDialog(getViewerPanel(), "The file, "+f.getName()+", already exists.\nDo you want to overwrite this file?", strTitle, JOptionPane.YES_NO_OPTION);
					//YES
					if(selectedIndex == 0) {
						createFile(tArea, strType, filter, f);
					}
				}else {
					createFile(tArea, strType, filter, f);
				}
		  	}
		}else {
			//overwrite the existing file
			createFile(tArea, strType, filter, file);
		}
	}

	private void createFile(JTextArea tArea, String strType, TExtFileFilter filter, File f) {
		if(!filter.accept(f)) {
			f = filter.appendExtension(f, ".txt");
		}
		if (filter.accept(f)) {
			try{
	            FileWriter fw = new FileWriter(f);
	            fw.write(tArea.getText() );
	            fw.close();
	            if(strType.charAt(0) == 'S') {
		            sourcePath = f.getPath();
	            }else {
		            targetPath = f.getPath();
	            }
				tArea.setBorder(new TitledBorder(strType + f.getName()));
			}catch(FileNotFoundException fnfe) {
			}catch(IOException ioe) {
			}catch(Exception ex) {
			}
		}else{
			System.out.println("File not accepted.");
		}
	}

	//cut
	public void cutAction(JTextArea tArea) {
		tArea.cut();
	}
	
	//copy
	public void copyAction(JTextArea tArea) {
		tArea.copy();
	}
	
	//paste
	public void pasteAction(JTextArea tArea) {
		tArea.paste();
	}
	
	//train
	public void trainAction() {
/*
		SwingUtilities.invokeLater(new TrainAction(this));
		trainThread = new Thread(new TrainAction(this));
		trainThread.start();
*/
		vTraceData = null;
		
		DBConnector.connect();
		startTraining();
		DBConnector.close();

		if(tabbedPane.indexOfTab("Trace") != -1) {
			traceAction();
		}
	}
	
	public void startTraining() {
		String strSourceLanguage = getSourceLanguage(),
			strTargetLanguage = getTargetLanguage(),
			strSourceText = getSourceText(),
			strTargetText = getTargetText();
		
		char cSourceLanguage = Character.toLowerCase(strSourceLanguage.charAt(0));
		char cTargetLanguage = Character.toLowerCase(strTargetLanguage.charAt(0));
		boolean bFilipinoSource;
		long lTotalIATime = 0;
		long lTotalTrainingTime = 0;

		if(cSourceLanguage == 'f') {
			bFilipinoSource = true;
		}else {
			bFilipinoSource = false;
		}

		//InputAnalysis
		InputAnalyzer ia = new InputAnalyzer();

		lTotalIATime = System.currentTimeMillis();
		Vector vAlignedSentences = ia.analyzeTrainInput(strSourceText, strTargetText, cTargetLanguage, "Regular Expression");
		lTotalIATime = System.currentTimeMillis() - lTotalIATime;

		int nSentenceNo = 0;

		//Learning
		if(txtLearner == null) {
			txtLearner = new TExtLearner();
		}

		if(vAlignedSentences != null) {
			nSentenceNo = vAlignedSentences.size();
			lTotalTrainingTime = System.currentTimeMillis();
			txtLearner.initiateTemplateLearning(vAlignedSentences, bFilipinoSource);
			lTotalTrainingTime = System.currentTimeMillis() - lTotalTrainingTime;
			vTraceData = txtLearner.readLearningTrace();
			txtLearner.clearLearningTrace();
			System.out.println("vTraceData: "+vTraceData);
		}else {
			//error message here!!!
		}

//		GUI.clearStatus();
		GUI.appendToStatus("Training Completed.");
		
		if (!bFilipinoSource) GUI.appendToStatus("\nEnglish to Filipino");
		else GUI.appendToStatus("Filipino to English");
		GUI.appendToStatus("Number of Sentences: " + nSentenceNo);
		GUI.appendToStatus("Total IA Time: " + lTotalIATime + " ms");
		GUI.appendToStatus("Total Training Time: " + lTotalTrainingTime + " ms");
		GUI.appendToStatus("Total IA & Training Time: " + (lTotalIATime + lTotalTrainingTime) + " ms");
	}
	
	//trace
	public void traceAction() {
		if(tabbedPane.indexOfTab("Trace") != -1) {
			tabbedPane.remove(tabbedPane.indexOfTab("Trace"));
		}

		traceTab = new JTabbedPane(JTabbedPane.TOP);
		traceTab.addTab("Extracted Templates", createExtractedTemplates());
		traceTab.addTab("Generated Chunks", createGeneratedChunks());

		tabbedPane.addTab("Trace", traceTab);

		tabbedPane.setSelectedComponent(traceTab);
	}

	//mode	
	public char getMode() {
		return caretMode;
	}

	public void setMode(char mode) {
		caretMode = mode;
	}

	//Text Area
	public JTextArea getSourceTextArea() {
		return sourceTextArea;
	}

	public JTextArea getTargetTextArea() {
		return targetTextArea;
	}

	//Text
	public void setSourceText(String s) {
		sourceTextArea.setText(s);
	}

	public void setTargetText(String s) {
		targetTextArea.setText(s);
	}

	public String getSourceText() {
		return sourceTextArea.getText();
	}

	public String getTargetText() {
		return targetTextArea.getText();
	}

	//Language
	public String getSourceLanguage() {
		return (String) sourceCombo.getModel().getSelectedItem();
	}

	public String getTargetLanguage() {
		return (String) targetCombo.getModel().getSelectedItem();
	}
/*
	public String getTrainThread() {
		return trainThread;
	}

	class TrainAction implements Runnable {
		TrainingModeViewer trainingModeViewer;
		
		public TrainAction(TrainingModeViewer trainingModeViewer) {
			this.trainingModeViewer = trainingModeViewer;
		}
	
		public void run() {
			if(Thread.currentThread() == trainingModeViewer.getTrainThread()) {
				trainingModeViewer.startTraining();
			}
		}
	}
*/
	class CaretAction implements CaretListener {
		TrainingModeViewer trainingModeViewer;
		
		public CaretAction(TrainingModeViewer trainingModeViewer) {
			this.trainingModeViewer = trainingModeViewer;
		}

		public void caretUpdate(CaretEvent e) {
			if(trainingModeViewer.getSourceTextArea().getCaret().isVisible()) {
				trainingModeViewer.setMode('S');
			}else if(trainingModeViewer.getTargetTextArea().getCaret().isVisible()) {
				trainingModeViewer.setMode('T');
			}else {
				trainingModeViewer.setMode('\0');
			}
		}
	}

	class ChangeAction implements DocumentListener {
		JTextArea tArea;
		
		public ChangeAction(JTextArea tArea) {
			this.tArea = tArea;
		}
	
		private void changeTitle() {
			if(!((TitledBorder)tArea.getBorder()).getTitle().endsWith("*")) {
				tArea.setBorder(new TitledBorder(((TitledBorder)tArea.getBorder()).getTitle() + "*"));
			}
		}		

		public void changedUpdate(DocumentEvent e) {
			changeTitle();
		}

		public void removeUpdate(DocumentEvent e) {
			changeTitle();
		}
		
		public void insertUpdate(DocumentEvent e) {
			changeTitle();
		}
	}
}